﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace WatiN.Samples.Console
{
    public class BaseUserConfigData
    {
        /// <summary>
        /// 登录电子邮件
        /// </summary>
        public string Email = string.Empty;

        /// <summary>
        /// 密码
        /// </summary>
        public string Password = string.Empty;

        /// <summary>
        /// 配置信息
        /// </summary>
        public IniConfig IniConf = null;
    }

    public class ConfigHelper
    {
        public const int SLEEP_TIME = 1000;

        private ConfigHelper()
        {
        }

        public static ConfigHelper ReadGlobalConfigData(string filename)
        {
            ConfigHelper config = new ConfigHelper();
            //try
            //{
            //    config.IniConf = IniHelper.ReadConfig(filename);

            //    IniField field;

            //    field = config.IniConf["system"] ?? config.IniConf[String.Empty];

            //    if (field != null)
            //    {
            //        config.ProxyServer = field["ProxyServer"] ?? config.ProxyServer;
            //        config.ProxyPort = PublicHelper.GetIntegerValue(field["ProxyPort"]) ?? config.ProxyPort;
            //        config.ProxyUser = field["ProxyUser"] ?? config.ProxyUser;
            //        config.ProxyPass = field["ProxyPass"] ?? config.ProxyPass;
            //        config.ConnectDelayTime = PublicHelper.GetIntegerValue(field["DelayConnect"]) ?? config.ConnectDelayTime;

            //        config.RepeatTime = PublicHelper.GetIntegerValue(field["RepeatTime"]) ?? config.RepeatTime;
            //        config.MidnightForce = PublicHelper.GetIntegerValue(field["MidnightForce"]) ?? config.MidnightForce;
            //        config.TryTimes = PublicHelper.GetIntegerValue(field["TryTimes"]) ?? config.TryTimes;
            //        config.HttpTimeout = (PublicHelper.GetIntegerValue(field["HttpTimeout"]) ?? config.HttpTimeout) * 1000;
            //        config.HttpTryTimes = PublicHelper.GetIntegerValue(field["HttpTryTimes"]) ?? config.HttpTryTimes;

            //        TraceLog.bWriteToFile = PublicHelper.GetBoolValue(field["EnableLog"]) ?? false;
            //    }
            //}
            //catch (Exception e)
            //{
            //    TraceLog.Error(e.Message);
            //    return null;
            //}

            return config;
        }

        public static BaseUserConfigData ReadUserConfigData(string filename)
        {
            BaseUserConfigData config = new BaseUserConfigData();
            try
            {
                config.IniConf = IniHelper.ReadConfig(filename);

                // 读取 [main] 段中的内容
                // 如果没有 [main] 段，则从默认段里读取配置信息（兼容以前的配置文件）
                IniField field = config.IniConf["main"] ?? config.IniConf[String.Empty];

                if (field != null)
                {
                    config.Email = field["email"];
                    config.Password = field["password"];
                }
            }
            catch (Exception e)
            {
                TraceLog.Error(e.Message);
                return null;
            }

            if (String.IsNullOrEmpty(config.Email) || String.IsNullOrEmpty(config.Password))
            {
                TraceLog.Error("文件格式不正确！");
                return null;
            }

            return config;
        }
    }
}
